CREATE table "USERS" (
    "USER_ID"    NUMBER NOT NULL,
    "USERNAME"   VARCHAR2(4000),
    "PASSWORD"   VARCHAR2(4000),
    "TYPE"       VARCHAR2(4000),
    "EMAIL"      VARCHAR2(4000),
    "ENABLED"    VARCHAR2(4000),
    constraint  "USERS_PK" primary key ("USER_ID")
)
/

CREATE sequence "USERS_SEQ" 
/

CREATE trigger "BI_USERS"  
  before insert on "USERS"              
  for each row 
begin  
    select "USERS_SEQ".nextval into :NEW.USER_ID from dual;
end;
/   

alter table "USERS" add
constraint USERS_CK1
check (TYPE in ('Web','Keypad','Master','Creator','Admin','SysAdmin'))
/   
alter table "USERS" add
constraint USERS_CK2
check (ENABLED in ('Yes', 'No'))
/   
  
CREATE table "USER_EXPIRY" (
    "EXPIRY_ID"   NUMBER NOT NULL,
    "EXPIRY_DATE" NUMBER NOT NULL,
    "USER_ID"     NUMBER NOT NULL,
    constraint  "USER_EXPIRY_PK" primary key ("EXPIRY_ID")
)
/

CREATE TABLE  "USER_TOTALS" 
   (	"USER_TYPE" VARCHAR2(4000) NOT NULL ENABLE, 
	"TOTAL" VARCHAR2(4000) NOT NULL ENABLE, 
	 CONSTRAINT "USER_TOTALS_PK" PRIMARY KEY ("USER_TYPE") ENABLE, 
	 CONSTRAINT "USER_TOTALS_CK1" CHECK (USER_TYPE IN ('Web','Master','Creator','Admin','SysAdmin')) ENABLE
   )
/

CREATE OR REPLACE TRIGGER  "BI_USER_TOTALS" 
  before insert on "USER_TOTALS"               
  for each row  
begin   
    select "USER_TOTALS_SEQ".nextval into :NEW.USER_TYPE from dual; 
end; 

/
ALTER TRIGGER  "BI_USER_TOTALS" ENABLE
/

CREATE table "POLL" (
    "POLL_ID"       NUMBER NOT NULL,
    "POLL_NAME"          VARCHAR2(4000) NOT NULL,
    "PUBLIC_RESULT" VARCHAR2(4000) NOT NULL,
    "ANONYMOUS"     VARCHAR2(4000) NOT NULL,
    "ADMIN_ID"      NUMBER,
    "ACTIVE_QUESTION" NUMBER,
    "CREATION_DATE"     NUMBER,
    "STATUS"        NUMBER,
    constraint  "POLL_PK" primary key ("POLL_ID")
)
/

alter table "POLL" add
constraint POLL_CK2
check (ANONYMOUS IN ('Yes','No'))
/   
alter table "POLL" add
constraint POLL_CK3
check (PUBLIC_RESULT IN ('Yes','No'))
/   

CREATE table "PARTICIPANT_LIST" (
    "LIST_ID"    NUMBER NOT NULL,
    "POLL_ID"    NUMBER NOT NULL,
    constraint  "PARTICIPANT_LIST_PK" primary key ("LIST_ID")
)
/

ALTER TABLE "PARTICIPANT_LIST" ADD CONSTRAINT "PARTICIPANT_LIST_FK" 
FOREIGN KEY ("POLL_ID")
REFERENCES "POLL" ("POLL_ID")

/

CREATE table "PARTICIPANT_LIST_TEMPLATE" (
    "TEMPLATE_ID" NUMBER,
    "NAME"        VARCHAR2(4000),
    constraint  "PARTICIPANT_LIST_TEMPLATE_PK" primary key ("TEMPLATE_ID")
)
/

CREATE sequence "PARTICIPANT_LIST_TEMPLATE_SEQ" 
/

CREATE trigger "BI_PARTICIPANT_LIST_TEMPLATE"  
  before insert on "PARTICIPANT_LIST_TEMPLATE"              
  for each row 
begin  
    select "PARTICIPANT_LIST_TEMPLATE_SEQ".nextval into :NEW.TEMPLATE_ID from dual;
end;
/   

CREATE table "DEMOGRAPHIC" (
    "DEMOGRAPHIC_ID" NUMBER NOT NULL,
    "NAME"           VARCHAR2(4000) NOT NULL,
    constraint  "DEMOGRAPHIC_PK" primary key ("DEMOGRAPHIC_ID")
)
/

CREATE sequence "DEMOGRAPHIC_SEQ" 
/

CREATE trigger "BI_DEMOGRAPHIC"  
  before insert on "DEMOGRAPHIC"              
  for each row 
begin  
    select "DEMOGRAPHIC_SEQ".nextval into :NEW.DEMOGRAPHIC_ID from dual;
end;
/   

CREATE table "DEMOGRAPHIC_TEMPLATE" (
    "DEMOGRAPHIC_TEMPLATE_ID" NUMBER,
    "NAME"                    VARCHAR2(4000),
    "TEMPLATE_ID"             NUMBER,
    constraint  "DEMOGRAPHIC_TEMPLATE_PK" primary key ("DEMOGRAPHIC_TEMPLATE_ID")
)
/

CREATE sequence "DEMOGRAPHIC_TEMPLATE_SEQ" 
/

CREATE trigger "BI_DEMOGRAPHIC_TEMPLATE"  
  before insert on "DEMOGRAPHIC_TEMPLATE"              
  for each row 
begin  
    select "DEMOGRAPHIC_TEMPLATE_SEQ".nextval into :NEW.DEMOGRAPHIC_TEMPLATE_ID from dual;
end;
/   

ALTER TABLE "DEMOGRAPHIC_TEMPLATE" ADD CONSTRAINT "DEMOGRAPHIC_TEMPLATE_FK" 
FOREIGN KEY ("TEMPLATE_ID")
REFERENCES "PARTICIPANT_LIST_TEMPLATE" ("TEMPLATE_ID")

/

CREATE table "PARTICIPANT" (
    "LIST_ID"    NUMBER NOT NULL,
    "USER_ID"    NUMBER NOT NULL,
    "DEMOGRAPHIC_ID"    NUMBER NOT NULL,
    constraint  "PARTICIPANT_PK" primary key ("LIST_ID","USER_ID","DEMOGRAPHIC_ID")
)
/

ALTER TABLE "PARTICIPANT" ADD CONSTRAINT "PARTICIPANT_FK" 
FOREIGN KEY ("LIST_ID")
REFERENCES "PARTICIPANT_LIST" ("LIST_ID")

/
ALTER TABLE "PARTICIPANT" ADD CONSTRAINT "PARTICIPANT_FK2" 
FOREIGN KEY ("USER_ID")
REFERENCES "USERS" ("USER_ID")

/
ALTER TABLE "PARTICIPANT" ADD CONSTRAINT "PARTICIPANT_FK3" 
FOREIGN KEY ("DEMOGRAPHIC_ID")
REFERENCES "DEMOGRAPHIC" ("DEMOGRAPHIC_ID")

/

CREATE table "PARTICIPANT_DETAIL" (
    "DETAIL_ID"  NUMBER NOT NULL,
    "KEY"        VARCHAR2(4000) NOT NULL,
    "VALUE"      VARCHAR2(4000) NOT NULL,
    "USER_ID"    NUMBER NOT NULL,
    constraint  "PARTICIPANT_DETAIL_PK" primary key ("DETAIL_ID")
)
/

CREATE sequence "PARTICIPANT_DETAIL_SEQ" 
/

CREATE trigger "BI_PARTICIPANT_DETAIL"  
  before insert on "PARTICIPANT_DETAIL"              
  for each row 
begin  
    select "PARTICIPANT_DETAIL_SEQ".nextval into :NEW.DETAIL_ID from dual;
end;
/   

ALTER TABLE "PARTICIPANT_DETAIL" ADD CONSTRAINT "PARTICIPANT_DETAIL_FK" 
FOREIGN KEY ("USER_ID")
REFERENCES "USERS" ("USER_ID")

/

CREATE table "LOCATION" (
    "LOCATION_ID" NUMBER NOT NULL,
    "NAME"        VARCHAR2(4000) NOT NULL,
    "LATITUDE"    VARCHAR2(4000) NOT NULL,
    "LONGITUDE"   VARCHAR2(4000) NOT NULL,
    constraint  "LOCATION_PK" primary key ("LOCATION_ID")
)
/

CREATE sequence "LOCATION_SEQ" 
/

CREATE trigger "BI_LOCATION"  
  before insert on "LOCATION"              
  for each row 
begin  
    select "LOCATION_SEQ".nextval into :NEW.LOCATION_ID from dual;
end;
/   

CREATE table "QUESTION" (
    "QUESTION_ID"   NUMBER NOT NULL,
    "QUESTION_TEXT" VARCHAR2(4000),
    "CREATION_DATE" NUMBER,
    "QUESTION_TYPE"          VARCHAR2(4000),
    "DELETED"       VARCHAR2(4000),
    "STATUS"        NUMBER,
    "OVERWRITTEN"   VARCHAR2(4000),
    "POLL_ID"       NUMBER,
    "CLOSE_TIME"    NUMBER,
    constraint  "QUESTION_PK" primary key ("QUESTION_ID")
)
/

alter table "QUESTION" add
constraint QUESTION_CK1
check (QUESTION_TYPE IN ('MC','SR'))
/   
alter table "QUESTION" add
constraint QUESTION_CK2
check (DELETED IN ('Yes','No'))
/   
alter table "QUESTION" add
constraint QUESTION_CK3
check (OVERWRITTEN IN ('Yes','No'))
/   

CREATE table "QUESTION_CLOSE" (
    "QUESTION_CLOSE_ID" NUMBER NOT NULL,
    "TIMESTAMP"         VARCHAR2(4000) NOT NULL,
    "QUESTION_ID"       NUMBER NOT NULL,
    constraint  "QUESTION_CLOSE_PK" primary key ("QUESTION_CLOSE_ID")
)
/

ALTER TABLE "QUESTION_CLOSE" ADD CONSTRAINT "QUESTION_CLOSE_FK" 
FOREIGN KEY ("QUESTION_ID")
REFERENCES "QUESTION" ("QUESTION_ID")

/

CREATE table "OBJECT" (
    "OBJECT_ID"   NUMBER NOT NULL,
    "HTML"        VARCHAR2(4000) NOT NULL,
    "QUESTION_ID" NUMBER NOT NULL,
    constraint  "OBJECT_PK" primary key ("OBJECT_ID")
)
/

CREATE sequence "OBJECT_SEQ" 
/

CREATE trigger "BI_OBJECT"  
  before insert on "OBJECT"              
  for each row 
begin  
    select "OBJECT_SEQ".nextval into :NEW.OBJECT_ID from dual;
end;
/   

ALTER TABLE "OBJECT" ADD CONSTRAINT "OBJECT_FK" 
FOREIGN KEY ("QUESTION_ID")
REFERENCES "QUESTION" ("QUESTION_ID")

/

CREATE table "MC_QUESTION" (
    "MC_QUESTION_ID" NUMBER NOT NULL,
    "STYLE"          NUMBER,
    "TEMPLATE"       VARCHAR2(4000),
    "RANKING"        VARCHAR2(4000),
    "DEMOGRAPHIC"    VARCHAR2(4000),
    "COMPARISON"     VARCHAR2(4000),
    "QUESTION_ID"    NUMBER NOT NULL,
    constraint  "MC_QUESTION_PK" primary key ("MC_QUESTION_ID")
)
/

CREATE sequence "MC_QUESTION_SEQ" 
/

CREATE trigger "BI_MC_QUESTION"  
  before insert on "MC_QUESTION"              
  for each row 
begin  
    select "MC_QUESTION_SEQ".nextval into :NEW.MC_QUESTION_ID from dual;
end;
/   

ALTER TABLE "MC_QUESTION" ADD CONSTRAINT "MC_QUESTION_FK" 
FOREIGN KEY ("QUESTION_ID")
REFERENCES "QUESTION" ("QUESTION_ID")

/
alter table "MC_QUESTION" add
constraint MC_QUESTION_CK1
check (TEMPLATE IN ('Yes','No'))
/   
alter table "MC_QUESTION" add
constraint MC_QUESTION_CK2
check (RANKING IN ('Yes','No'))
/   
alter table "MC_QUESTION" add
constraint MC_QUESTION_CK3
check (DEMOGRAPHIC IN ('Yes','No'))
/   
alter table "MC_QUESTION" add
constraint MC_QUESTION_CK4
check (COMPARISON IN ('Yes','No'))
/   

CREATE table "MC_ANSWER" (
    "MC_ANSWER_ID" NUMBER,
    "ANSWER_TEXT"  VARCHAR2(4000),
    "CORRECT"      VARCHAR2(4000),
    "WEIGHTING"    NUMBER,
    "DELETED"      VARCHAR2(4000),
    "QUESTION_ID"  NUMBER,
    constraint  "MC_ANSWER_PK" primary key ("MC_ANSWER_ID")
)
/

CREATE sequence "MC_ANSWER_SEQ" 
/

CREATE trigger "BI_MC_ANSWER"  
  before insert on "MC_ANSWER"              
  for each row 
begin  
    select "MC_ANSWER_SEQ".nextval into :NEW.MC_ANSWER_ID from dual;
end;
/   

ALTER TABLE "MC_ANSWER" ADD CONSTRAINT "MC_ANSWER_FK" 
FOREIGN KEY ("QUESTION_ID")
REFERENCES "QUESTION" ("QUESTION_ID")

/
alter table "MC_ANSWER" add
constraint MC_ANSWER_CK1
check (CORRECT IN ('Yes','No'))
/   
alter table "MC_ANSWER" add
constraint MC_ANSWER_CK2
check (DELETED IN ('Yes','No'))
/   

CREATE table "MC_RESPONSE" (
    "MC_RESPONSE_ID" NUMBER NOT NULL,
    "ANSWER_ID"      NUMBER NOT NULL,
    "PREFERENCE"     VARCHAR2(4000) NOT NULL,
    "QUESTION_ID"    NUMBER NOT NULL,
    "USER_ID"        NUMBER NOT NULL,
    constraint  "MC_RESPONSE_PK" primary key ("MC_RESPONSE_ID")
)
/

ALTER TABLE "MC_RESPONSE" ADD CONSTRAINT "MC_RESPONSE_FK" 
FOREIGN KEY ("QUESTION_ID")
REFERENCES "QUESTION" ("QUESTION_ID")
/

ALTER TABLE "MC_RESPONSE" ADD CONSTRAINT "MC_RESPONSE_FK2" 
FOREIGN KEY ("USER_ID")
REFERENCES "USERS" ("USER_ID")
/

ALTER TABLE "MC_RESPONSE" ADD CONSTRAINT "MC_RESPONSE_FK3" 
FOREIGN KEY ("ANSWER_ID")
REFERENCES "MC_ANSWER" ("MC_ANSWER_ID")
/

CREATE table "SR_QUESTION" (
    "SQ_QUESTION_ID" NUMBER NOT NULL,
    "QUESTION_TYPE"  VARCHAR2(4000) NOT NULL,
    "QUESTION_ID"    NUMBER NOT NULL,
    constraint  "SR_QUESTION_PK" primary key ("SQ_QUESTION_ID")
)
/

CREATE sequence "SR_QUESTION_SEQ" 
/

CREATE trigger "BI_SR_QUESTION"  
  before insert on "SR_QUESTION"              
  for each row 
begin  
    select "SR_QUESTION_SEQ".nextval into :NEW.SQ_QUESTION_ID from dual;
end;
/   

ALTER TABLE "SR_QUESTION" ADD CONSTRAINT "SR_QUESTION_FK" 
FOREIGN KEY ("QUESTION_ID")
REFERENCES "QUESTION" ("QUESTION_ID")

/
alter table "SR_QUESTION" add
constraint SR_QUESTION_CK1
check (QUESTION_TYPE IN ('Numeric','Alphanumeric'))
/   

CREATE table "SR_KEYWORD" (
    "SR_KEYWORD_ID" NUMBER NOT NULL,
    "KEYWORD"       VARCHAR2(4000) NOT NULL,
    "DELETED"       VARCHAR2(4000) NOT NULL,
    "QUESTION_ID"   NUMBER NOT NULL,
    constraint  "SR_KEYWORD_PK" primary key ("SR_KEYWORD_ID")
)
/

CREATE sequence "SR_KEYWORD_SEQ" 
/

CREATE trigger "BI_SR_KEYWORD"  
  before insert on "SR_KEYWORD"              
  for each row 
begin  
    select "SR_KEYWORD_SEQ".nextval into :NEW.SR_KEYWORD_ID from dual;
end;
/   

ALTER TABLE "SR_KEYWORD" ADD CONSTRAINT "SR_KEYWORD_FK" 
FOREIGN KEY ("QUESTION_ID")
REFERENCES "QUESTION" ("QUESTION_ID")

/
alter table "SR_KEYWORD" add
constraint SR_KEYWORD_CK1
check (DELETED IN ('Yes','No'))
/   

CREATE table "SR_RESPONSE" (
    "SR_RESPONSE_ID" NUMBER NOT NULL,
    "RESPONSE_TEXT"  VARCHAR2(4000) NOT NULL,
    "QUESTION_ID"    NUMBER NOT NULL,
    "USER_ID"        NUMBER NOT NULL,
    constraint  "SR_RESPONSE_PK" primary key ("SR_RESPONSE_ID")
)
/

CREATE sequence "SR_RESPONSE_SEQ" 
/

CREATE trigger "BI_SR_RESPONSE"  
  before insert on "SR_RESPONSE"              
  for each row 
begin  
    select "SR_RESPONSE_SEQ".nextval into :NEW.SR_RESPONSE_ID from dual;
end;
/   

ALTER TABLE "SR_RESPONSE" ADD CONSTRAINT "SR_RESPONSE_FK" 
FOREIGN KEY ("QUESTION_ID")
REFERENCES "QUESTION" ("QUESTION_ID")

/
ALTER TABLE "SR_RESPONSE" ADD CONSTRAINT "SR_RESPONSE_FK2" 
FOREIGN KEY ("USER_ID")
REFERENCES "USERS" ("USER_ID")

/

CREATE table "MESSAGE" (
    "MESSAGE_ID"  NUMBER NOT NULL,
    "TEXT"        VARCHAR2(4000) NOT NULL,
    "PRIVATE"     VARCHAR2(4000) NOT NULL,
    "DELETED"     VARCHAR2(4000) NOT NULL,
    "READ"        VARCHAR2(4000) NOT NULL,
    "SENDER_ID"   NUMBER NOT NULL,
    "RECEIVER_ID" NUMBER NOT NULL,
    constraint  "MESSAGE_PK" primary key ("MESSAGE_ID")
)
/

CREATE sequence "MESSAGE_SEQ" 
/

CREATE trigger "BI_MESSAGE"  
  before insert on "MESSAGE"              
  for each row 
begin  
    select "MESSAGE_SEQ".nextval into :NEW.MESSAGE_ID from dual;
end;
/   

ALTER TABLE "MESSAGE" ADD CONSTRAINT "MESSAGE_FK" 
FOREIGN KEY ("SENDER_ID")
REFERENCES "USERS" ("USER_ID")

/
ALTER TABLE "MESSAGE" ADD CONSTRAINT "MESSAGE_FK2" 
FOREIGN KEY ("RECEIVER_ID")
REFERENCES "USERS" ("USER_ID")

/
alter table "MESSAGE" add
constraint MESSAGE_CK1
check (PRIVATE IN ('Yes','No'))
/   
alter table "MESSAGE" add
constraint MESSAGE_CK2
check (DELETED IN ('Yes','No'))
/   
alter table "MESSAGE" add
constraint MESSAGE_CK3
check (READ IN ('Yes','No'))
/   

CREATE table "ORGANISATION" (
    "ORGANISATION_ID" NUMBER,
    "NAME"            VARCHAR2(4000),
    constraint  "ORGANISATION_PK" primary key ("ORGANISATION_ID")
)
/

CREATE sequence "ORGANISATION_SEQ" 
/

CREATE trigger "BI_ORGANISATION"  
  before insert on "ORGANISATION"              
  for each row 
begin  
    select "ORGANISATION_SEQ".nextval into :NEW.ORGANISATION_ID from dual;
end;
/   

CREATE table "COMPARISON" (
    "POLL_ID_1"  NUMBER,
    "POLL_ID_2"  NUMBER,
    constraint  "COMPARISON_PK" primary key ("POLL_ID_1", "POLL_ID_2")
)
/

CREATE sequence "COMPARISON_SEQ" 
/

CREATE trigger "BI_COMPARISON"  
  before insert on "COMPARISON"              
  for each row 
begin  
    select "COMPARISON_SEQ".nextval into :NEW.POLL_ID_1 from dual;
end;
/   

ALTER TABLE "COMPARISON" ADD CONSTRAINT "COMPARISON_FK" 
FOREIGN KEY ("POLL_ID_1")
REFERENCES "POLL" ("POLL_ID")

/
ALTER TABLE "COMPARISON" ADD CONSTRAINT "COMPARISON_FK2" 
FOREIGN KEY ("POLL_ID_2")
REFERENCES "POLL" ("POLL_ID")

/

CREATE table "OVERWRITTEN_QUESTIONS" (
    "QUESTION_ID_OLD"  NUMBER,
    "QUESTION_ID_NEW"  NUMBER,
    constraint  "OVERWRITTEN_QUESTIONS_PK" primary key ("QUESTION_ID_OLD", "QUESTION_ID_NEW")
)
/

CREATE sequence "OVERWRITTEN_QUESTIONS_SEQ" 
/

CREATE trigger "BI_OVERWRITTEN_QUESTIONS"  
  before insert on "OVERWRITTEN_QUESTIONS"              
  for each row 
begin  
    select "OVERWRITTEN_QUESTIONS_SEQ".nextval into :NEW.QUESTION_ID_OLD from dual;
end;
/   

ALTER TABLE "OVERWRITTEN_QUESTIONS" ADD CONSTRAINT "OVERWRITTEN_QUESTIONS_FK" 
FOREIGN KEY ("QUESTION_ID_OLD")
REFERENCES "QUESTION" ("QUESTION_ID")

/
ALTER TABLE "OVERWRITTEN_QUESTIONS" ADD CONSTRAINT "OVERWRITTEN_QUESTIONS_FK2" 
FOREIGN KEY ("QUESTION_ID_NEW")
REFERENCES "QUESTION" ("QUESTION_ID")

/

CREATE table "POLL_CREATOR" (
    "POLL_ID"  NUMBER,
    "CREATOR_ID"  NUMBER,
    constraint  "POLL_CREATOR_PK" primary key ("POLL_ID", "CREATOR_ID")
)
/

CREATE sequence "POLL_CREATOR_SEQ" 
/

CREATE trigger "BI_POLL_CREATOR"  
  before insert on "POLL_CREATOR"              
  for each row 
begin  
    select "POLL_CREATOR_SEQ".nextval into :NEW.POLL_ID from dual;
end;
/   

--ALTER TABLE "POLL_CREATOR" ADD CONSTRAINT "POLL_CREATOR_FK" 
--FOREIGN KEY ("POLL_ID")
--REFERENCES "POLL" ("POLL_ID")
--/

--ALTER TABLE "POLL_CREATOR" ADD CONSTRAINT "POLL_CREATOR_FK2" 
--FOREIGN KEY ("CREATOR_ID")
--REFERENCES "USERS" ("USER_ID")

--/

CREATE table "POLL_MASTER" (
    "POLL_ID"    NUMBER NOT NULL,
    "MASTER_ID" NUMBER NOT NULL
)
/

alter table "POLL_MASTER" add constraint  "POLL_MASTER_PK" primary key ("POLL_ID","MASTER_ID")
/

ALTER TABLE "POLL_MASTER" ADD CONSTRAINT "POLL_MASTER_FK" 
FOREIGN KEY ("POLL_ID")
REFERENCES "POLL" ("POLL_ID")

/
ALTER TABLE "POLL_MASTER" ADD CONSTRAINT "POLL_MASTER_FK2" 
FOREIGN KEY ("MASTER_ID")
REFERENCES "USERS" ("USER_ID")

/


CREATE table "USER_DEMOGRAPHIC" (
    "USER_ID"  NUMBER,
    "DEMOGRAPHIC_ID"  NUMBER,
    constraint  "USER_DEMOGRAPHIC_PK" primary key ("USER_ID", "DEMOGRAPHIC_ID")
)
/

CREATE sequence "USER_DEMOGRAPHIC_SEQ" 
/

CREATE trigger "BI_USER_DEMOGRAPHIC"  
  before insert on "USER_DEMOGRAPHIC"              
  for each row 
begin  
    select "USER_DEMOGRAPHIC_SEQ".nextval into :NEW.USER_ID from dual;
end;
/   

ALTER TABLE "USER_DEMOGRAPHIC" ADD CONSTRAINT "USER_DEMOGRAPHIC_FK" 
FOREIGN KEY ("USER_ID")
REFERENCES "USERS" ("USER_ID")

/
ALTER TABLE "USER_DEMOGRAPHIC" ADD CONSTRAINT "USER_DEMOGRAPHIC_FK2" 
FOREIGN KEY ("DEMOGRAPHIC_ID")
REFERENCES "DEMOGRAPHIC" ("DEMOGRAPHIC_ID")

/

CREATE table "LIST_DEMOGRAPHIC" (
    "LIST_ID"  NUMBER,
    "DEMOGRAPHIC_ID"  NUMBER,
    constraint  "LIST_DEMOGRAPHIC_PK" primary key ("LIST_ID", "DEMOGRAPHIC_ID")
)
/

CREATE sequence "LIST_DEMOGRAPHIC_SEQ" 
/

CREATE trigger "BI_LIST_DEMOGRAPHIC"  
  before insert on "LIST_DEMOGRAPHIC"              
  for each row 
begin  
    select "LIST_DEMOGRAPHIC_SEQ".nextval into :NEW.LIST_ID from dual;
end;
/   

ALTER TABLE "LIST_DEMOGRAPHIC" ADD CONSTRAINT "LIST_DEMOGRAPHIC_FK" 
FOREIGN KEY ("LIST_ID")
REFERENCES "PARTICIPANT_LIST" ("LIST_ID")

/
ALTER TABLE "LIST_DEMOGRAPHIC" ADD CONSTRAINT "LIST_DEMOGRAPHIC_FK2" 
FOREIGN KEY ("DEMOGRAPHIC_ID")
REFERENCES "DEMOGRAPHIC" ("DEMOGRAPHIC_ID")

/

CREATE table "USER_ORGANISATION" (
    "USER_ID"  NUMBER,
    "ORGANISATION_ID"  NUMBER,
    constraint  "USER_ORGANISATION_PK" primary key ("USER_ID", "ORGANISATION_ID")
)
/

CREATE sequence "USER_ORGANISATION_SEQ" 
/

CREATE trigger "BI_USER_ORGANISATION"  
  before insert on "USER_ORGANISATION"              
  for each row 
begin  
    select "USER_ORGANISATION_SEQ".nextval into :NEW.USER_ID from dual;
end;
/   

ALTER TABLE "USER_ORGANISATION" ADD CONSTRAINT "USER_ORGANISATION_FK" 
FOREIGN KEY ("USER_ID")
REFERENCES "USERS" ("USER_ID")

/
ALTER TABLE "USER_ORGANISATION" ADD CONSTRAINT "USER_ORGANISATION_FK2" 
FOREIGN KEY ("ORGANISATION_ID")
REFERENCES "ORGANISATION" ("ORGANISATION_ID")

/

CREATE table "USER_LOCATION" (
    "USER_ID"  NUMBER,
    "LOCATION_ID"  NUMBER,
    constraint  "USER_LOCATION_PK" primary key ("USER_ID", "LOCATION_ID")
)
/

CREATE sequence "USER_LOCATION_SEQ" 
/

CREATE trigger "BI_USER_LOCATION"  
  before insert on "USER_LOCATION"              
  for each row 
begin  
    select "USER_LOCATION_SEQ".nextval into :NEW.USER_ID from dual;
end;
/   

ALTER TABLE "USER_LOCATION" ADD CONSTRAINT "USER_LOCATION_FK" 
FOREIGN KEY ("USER_ID")
REFERENCES "USERS" ("USER_ID")

/
ALTER TABLE "USER_LOCATION" ADD CONSTRAINT "USER_LOCATION_FK2" 
FOREIGN KEY ("LOCATION_ID")
REFERENCES "LOCATION" ("LOCATION_ID")

/

CREATE table "POLL_LOCATION" (
    "POLL_ID"     NUMBER NOT NULL,
    "LOCATION_ID" NUMBER NOT NULL
)
/

alter table "POLL_LOCATION" add constraint  "POLL_LOCATION_PK" primary key ("POLL_ID","LOCATION_ID")
/

ALTER TABLE "POLL_LOCATION" ADD CONSTRAINT "POLL_LOCATION_FK" 
FOREIGN KEY ("POLL_ID")
REFERENCES "POLL" ("POLL_ID")

/
ALTER TABLE "POLL_LOCATION" ADD CONSTRAINT "POLL_LOCATION_FK2" 
FOREIGN KEY ("LOCATION_ID")
REFERENCES "LOCATION" ("LOCATION_ID")

/

CREATE TABLE "FEEDBACK" (
    "FEEDBACK_ID" NUMBER NOT NULL,
    "TEXT"        VARCHAR2(4000) NOT NULL,
    "QUESTION_ID" NUMBER NOT NULL
)
/

alter table "FEEDBACK" add constraint  "FEEDBACK_PK" primary key ("FEEDBACK_ID")
/

ALTER TABLE "FEEDBACK" ADD CONSTRAINT "FEEDBACK_FK" 
FOREIGN KEY ("QUESTION_ID")
REFERENCES "QUESTION" ("QUESTION_ID")
/

CREATE TABLE "SESSION" (
    "SESSION_ID" NUMBER NOT NULL,
    "SESSION_START" NUMBER,
    "SESSION_END" NUMBER,
    "POLL_ID" NUMBER
)
/

alter table "SESSION" add constraint  "SESSION_PK" primary key ("SESSION_ID")
/

ALTER TABLE "SESSION" ADD CONSTRAINT "SESSION_FK" 
FOREIGN KEY ("POLL_ID")
REFERENCES "POLL" ("POLL_ID")
/

commit;